package algorithm_optimal_solution.StackAndQueue;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

//求最大子矩阵的大小：先将二维数组变成一维数组（向下合并），然后使用单调栈求解每一位的扩展面积（面积 = (rightLess - leftLess -1)* 弹栈位高度height[h]）；
//如右边最近小位为-1，那么rightLess为数组长度；即使值有重复，也需要进行弹栈，准确面积以最后一位重复值弹栈计算为准。
public class MaxRectangle {

    public void maxSize(int[][] rectangle){
        int[] height = new int[rectangle[0].length];
        int maxArea = Integer.MIN_VALUE;
        for(int i = 0;i<rectangle.length;i++){
            for(int j = 0;j<rectangle[i].length;j++){
                height[j] = rectangle[i][j] == 0 ? 0 : height[j] + rectangle[i][j];
            }
            maxArea = Math.max(maxArea(height),maxArea);
        }
        System.out.println(maxArea);
    }

    private int maxArea(int[] height){
        Stack<Integer> stack = new Stack<>();
        int maxArea = Integer.MIN_VALUE;
        for(int i = 0;i<height.length;i++){
            while(!stack.isEmpty() && height[stack.peek()] >= height[i]){
                int h = stack.pop();
                int leftLess = stack.isEmpty() ? -1 : stack.peek();
                int rightLess = i;
                int area = (rightLess - leftLess - 1) * height[h];
                maxArea = Math.max(maxArea,area);
            }
            stack.push(i);
        }
        while(!stack.isEmpty()){
            int h = stack.pop();
            int leftLess = stack.isEmpty() ? -1 : stack.peek();
            int area = (height.length - leftLess - 1) * height[h];
            maxArea = Math.max(maxArea,area);
        }
        return maxArea;
    }

    public static void main(String[] args)throws IOException {
        BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
        String[] value = scanner.readLine().split(" ");
        int height = Integer.parseInt(value[0]);
        int width = Integer.parseInt(value[1]);
        int[][] rectangle = new int[height][width];
        for(int i = 0;i<height;i++){
            String[] numbers = scanner.readLine().split(" ");
            for(int j = 0;j<width;j++){
                rectangle[i][j] = Integer.parseInt(numbers[j]);
            }
        }
        new MaxRectangle().maxSize(rectangle);
    }
}
